package cn.kuaipai.weibo.http;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cn.kuaipai.weibo.constants.HttpMethod;
import cn.kuaipai.weibo.model.OAuth;
import cn.kuaipai.weibo.model.WeiboParameter;

/**
 * <p>微博资源</p>
 * 
 * @author zy
 */
public class WeiboRequest {
	protected final Logger logger = Logger.getLogger(this.getClass());
	private static final WeiboHttpClient weiboHttpClient = new WeiboHttpClient();
	private static final OAuthProvider oauthProvider = new OAuthProvider();

	/**
	 * 获取API返回结果(oauth1.0)
	 * 
	 * @param url  			接口URL
	 * @param parameters	请求所需参数
	 * @param oauth			授权对象
	 * @return				返回结果
	 */
	protected String getWeiboResource(String url, List<WeiboParameter> parameters,OAuth oauth){
		try{
			if(parameters == null)
				parameters = new ArrayList<WeiboParameter>();
			parameters.addAll(oauth.getAccessParameters());
			String queryString = oauthProvider.constructionOauthParameters(url,
					HttpMethod.GET.getValue(), oauth.getOauthConsumerSecret(),
					oauth.getOauthTokenSecret(), parameters);
			return weiboHttpClient.httpGet(url, queryString);
		}catch (Exception e) {
			logger.error("[weibo] getWeiboResource error url=" + url,e);
		}
		return null;
	}
	
	/**
	 * getResuource (通过OPENID实现)
	 * @param url		 String
	 * @param httpMethod String
	 * @param parameters List
	 * @param openid	 String
	 * @param openkey	 String
	 * 
	 * @return 返回结果 String
	 */
	protected String getWeiboResource(String url,List<WeiboParameter> parameters,String openid,String openkey){
		try{
			if(parameters == null)
				parameters = new ArrayList<WeiboParameter>();
			String queryString = oauthProvider.constructionOpenIdKeyParameters(url,HttpMethod.GET.getValue(), openid, openkey, parameters);
			return weiboHttpClient.httpGet(url, queryString);
		}catch (Exception e) {
			logger.error("[weibo] getWeiboResource error url=" + url,e);
		}
		return null;
	}
	/**
	 * Post Content (通过OPENID实现)
	 * @param url		 String
	 * @param httpMethod String
	 * @param parameters List
	 * @param openid	 String
	 * @param openkey	 String
	 * 
	 * @return 返回结果 String
	 */
	protected String weiboPostContent(String url,List<WeiboParameter> parameters,String openid,String openkey){
		try{
			if(parameters == null)
				parameters = new ArrayList<WeiboParameter>();
			String queryString = oauthProvider.constructionOpenIdKeyParameters(url,HttpMethod.POST.getValue(), openid, openkey, parameters);
			return weiboHttpClient.httpPost(url, queryString);
		}catch (Exception e) {
			logger.error("[weibo] weiboPostContent error url=" + url,e);
		}
		return null;
	}
	
	/**
	 * Post Content with Files (通过OPENID实现)
	 * @param url		 String
	 * @param httpMethod String
	 * @param parameters List   参数
	 * @param files 	 List   文件参数
	 * @param openid	 String
	 * @param openkey	 String
	 * 
	 * @return 返回结果 String
	 */
	protected String weiboPostContentFile(String url,List<WeiboParameter> parameters,List<WeiboParameter> files,String openid,String openkey){
		try{
			if(files == null)
				files = new ArrayList<WeiboParameter>();
			String queryString = oauthProvider.constructionOpenIdKeyParameters(url,HttpMethod.POST.getValue(), openid, openkey, parameters);
			return weiboHttpClient.httpPostWithFile(url, queryString, files);
		}catch (Exception e) {
			logger.error("[weibo] weiboPostContentFile error url=" + url,e);
		}
		return null;
	}
}
